﻿using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Regions;
using StockholmsStad.Windows.UI.Infrastructure.Events;
using StockholmsStad.Windows.UI.Modules.Ceremony.Controllers;
using StockholmsStad.Windows.UI.Modules.Ceremony.Interfaces;
using StockholmsStad.Windows.UI.Modules.Ceremony.Presenters;
using StockholmsStad.Windows.UI.Modules.Ceremony.Services;
using StockholmsStad.Windows.UI.Modules.Ceremony.Views;

namespace StockholmsStad.Windows.UI.Modules.Ceremony
{
    public class CeremonyModule : IModule
    {
        private readonly IUnityContainer container;
        private readonly IRegionManager regionManager;
        private readonly IEventAggregator eventAggregator;

        public CeremonyModule(IUnityContainer container, IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            this.container = container;
            this.regionManager = regionManager;
            this.eventAggregator = eventAggregator;
        }

        private void RegisterViewsAndServices()
        {
            container.RegisterType<ICeremonyController, CeremonyController>();
            container.RegisterType<ICeremonyService, CeremonyService>();

            container.RegisterType<ICeremonyView, CeremonyView>();

            container.RegisterType<ICeremonyListView, CeremonyListView>();
            container.RegisterType<ICeremonyListPresenter, CeremonyListPresenter>();

            container.RegisterType<ICeremonyDetailsView, CeremonyDetailsView>();
            container.RegisterType<ICeremonyDetailsPresenter, CeremonyDetailsPresenter>();
        }

        public void LoadView()
        {
            var args = new UIEventArgs { ShellTitle = "Parvis - Ceremonier" };
            eventAggregator.GetEvent<UIEvent>().Publish(args);

            var couplesPresenter = container.Resolve<CeremonyPresenter>();
            var contentRegion = regionManager.Regions[Infrastructure.RegionNames.ContentRegion];
            var loadedView = contentRegion.GetView(ViewNames.ListView);

            if (loadedView == null)
                contentRegion.Add(couplesPresenter.View, ViewNames.ListView);

            contentRegion.Activate(couplesPresenter.View);
        }

        #region IModule Members

        public void Initialize()
        {
            RegisterViewsAndServices();
        }

        #endregion
    }
}
